package com.zj.osm.utils.db_utils;

import android.annotation.SuppressLint;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.zj.osm.entity.greendao.Equipment;
import com.zj.osm.model.pojo.Equip;
import com.zj.osm.model.pojo.ModuleType;
import com.zj.osm.utils.SQLiteDatabaseUtils;

import java.util.ArrayList;
import java.util.List;

import static com.zj.osm.App.getInstances;

/**
 * <pre>
 *     author : luhenchang
 *     e-mail : 1276998208@qq.com
 *     time   : 2019/12/03
 *     desc   : 版权所有
 *     version: 1.0
 * </pre>
 */
public class EquipUtils {
    private static final String SELECT_FROM_EQUIP="select * from EquipType where component=";
    private static final EquipUtils ourInstance = new EquipUtils();
    public static EquipUtils getInstance() {
        return ourInstance;
    }

    private EquipUtils() {
    }
    /**
     *
     * @param str
     * @param wherestr
     * @return
     */
    public List<Equip> getEquip(int str, String wherestr) {
        List<Equip> strings = new ArrayList<>();
        SQLiteDatabase sqLiteDatabase = SQLiteDatabaseUtils.getInstance().getSQLiteDatabase();
        @SuppressLint("Recycle")
        Cursor cursor = sqLiteDatabase.rawQuery( "select * from EquipType where component="+ str + " order by ID", null);//此处的db是表名，name是列名
        while (cursor.moveToNext()) {
            if (cursor.getString(1).contains(wherestr)) {
                Equip equip = new Equip();
                equip.setId(cursor.getInt(0));
                equip.setName(cursor.getString(1));
                equip.setCode(cursor.getString(4));
                strings.add(equip);
            }

        }
        cursor.close();
        return strings;
    }
    /**
     * 先去 equipModleType= equipId根据 defalut 如果是 1 默认填入里面。   modleType表里面ModuleTypeId  =>
     *
     * @param equipId 类型
     * @return /
     */
    public ArrayList<ModuleType> selectedEquipModleType(int equipId) {
        SQLiteDatabase sqLiteDatabase = SQLiteDatabaseUtils.getInstance().getSQLiteDatabase();
        ArrayList<ModuleType> moduleTypes = new ArrayList<>();
        assert sqLiteDatabase != null;
        @SuppressLint("Recycle") Cursor cursor = sqLiteDatabase.rawQuery("select mt.* from ModuleType mt left join equip_moduleType em on em.moduleTypeID = mt.ModuleTypeID where em.equipID = '" + equipId + "' AND em.isDefault = '1'", null);//此处的db是表名，name是列名
        while (cursor.moveToNext()) {
            ModuleType moduleTypend = new ModuleType();
            moduleTypend.setModuleTypeID(cursor.getString(0));
            moduleTypend.setName(cursor.getString(1));
            moduleTypend.setModuleTypeKey(cursor.getString(2));
            moduleTypend.setParentKey(cursor.getString(3));
            moduleTypes.add(moduleTypend);
        }
        cursor.close();
        return moduleTypes;
    }
    /**
     * 获取数据库中的ID
     *
     * @param str 根据表中名称进行判断
     * @return 返回对应ID
     */
    public int getId(String str) {
        int id = 0;
        SQLiteDatabase db = SQLiteDatabaseUtils.getInstance().getSQLiteDatabase();
        assert db != null;
        @SuppressLint("Recycle") Cursor cursor = db.rawQuery("select * from EquipType where name=" + "'" + str + "'", null);//此处的db是表名，name是列名
        while (cursor.moveToNext()) {
            id = cursor.getInt(0);
        }
        cursor.close();
        return id;
    }
    /**
     * 添加杆塔设备
     *
     * @param name 设备名称
     * @param cid  设备ID
     */
    public void addEquipment(String name, Long cid, int flag) {
        Equipment equipment = new Equipment();
        equipment.setName(name);
        equipment.setPointId(cid);
        equipment.setId(null);
        equipment.setQubie(flag);
        equipment.setAmount(1);
        getInstances().getDaoSession().getEquipmentDao().insert(equipment);
    }
    /**
     * 添加模块设备和柱上设备
     *
     * @param id             点的id
     * @param moduleZhuzData 数据源
     * @param i              区别  0为模块设备 2为柱上设备
     */
    public void addEquipment(long projectId,Long id, ArrayList<ModuleType> moduleZhuzData, int i) {
        for (ModuleType moduleType : moduleZhuzData) {
            Equipment equipment = new Equipment();
            equipment.setLever_id(moduleType.getModuleTypeKey());
            equipment.setModule_code(moduleType.getModuleCode());
            equipment.setPointId(id);
            equipment.setProjectId(projectId);
            equipment.setQubie(i);
            equipment.setName(moduleType.getName());
            getInstances().getDaoSession().getEquipmentDao().insert(equipment);
        }
    }
    /**
     * 先去 equipModleType= equipId根据 defalut 如果是 1 默认填入里面。   modleType表里面ModuleTypeId  =>
     *
     * @param equipId /
     * @return 设备类型
     */
    public ArrayList<ModuleType> selectedEquipModleTypeZhuzhang(int equipId) {
        SQLiteDatabase sqLiteDatabase = SQLiteDatabaseUtils.getInstance().getSQLiteDatabase();
        ArrayList<ModuleType> moduleTypes = new ArrayList<>();
        assert sqLiteDatabase != null;
        @SuppressLint("Recycle") Cursor cursor = sqLiteDatabase.rawQuery("select mt.* from ModuleType mt left join \"equip_moduleType_ Attached\" em on em.equipTypeID = mt.ModuleTypeID where em.equipTypeID = '" + equipId + "' AND em.isDefault = '1';", null);//此处的db是表名，name是列名
        while (cursor.moveToNext()) {
            ModuleType moduleTypend = new ModuleType();
            moduleTypend.setModuleTypeID(cursor.getString(0));
            moduleTypend.setName(cursor.getString(1));
            moduleTypend.setModuleTypeKey(cursor.getString(2));
            moduleTypend.setParentKey(cursor.getString(3));
            moduleTypes.add(moduleTypend);
        }
        cursor.close();
        return moduleTypes;
    }
}
